package com.fdm.controller;

import java.sql.SQLException;

import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.fdm.model.Customer;
import com.fdm.model.CustomerRoles;
import com.fdm.model.dao.service.CustomerService;
import com.fdm.util.DateFormatter;
import com.fdm.util.exceptions.UniqueConstraintException;

@Controller
public class CustomerController {
 	//private DAOFactory dao;
	
	//@Autowired
 	//private CustomerDAO customerDAO;
 	
	@Autowired
 	private CustomerService customerService;
 	
	@RequestMapping(value="/register.do", method= RequestMethod.POST)
	public String registerUser(@RequestParam("firstname") String firstname, @RequestParam("lastname") String lastname, 
			@RequestParam("email") String email, @RequestParam("password") String password, @RequestParam("dob") String dob, Model model) throws UniqueConstraintException{
		Customer customer=new Customer();
		customer.setFirstname(firstname);
		customer.setLastname(lastname);
		customer.setEmail(email);
		customer.setPassword(password);
		//customer.setEnabled("true");
		customer.setDob(DateFormatter.formatStringToDate(dob));
		CustomerRoles a=new CustomerRoles();
		a.setAuthority("ROLE_USER");
		customer.setCustomerRoles(a);
		model.addAttribute("email", customer.getEmail());
		String view=customerService.createCustomer(customer);
		return view;
		/*try{
			customerDAO.createNewCustomer(customer);
		}
		catch(SQLException sqle){
			return "user_exists";
		}*
		return "done";
		/*try{
		    dao = DAOFactory.getDAOFactory(2);
		    dao.getCustomerDAO().createNewCustomer(customer);
		}
		catch(SQLException sqle){
			
		}*/
		
		//return "LibraryWebApplication/index";
	}
	 
}
